package com.hemaapp.yjnh.fragment;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseFragment;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.SearchActivity;
import com.hemaapp.yjnh.adapter.RecommendAdapter;
import com.hemaapp.yjnh.bean.YijiaRecommend;

/**易家推荐
 * @author GuiyouZhang
 * Date：2015年12月22日 下午1:05:15
 * Email:guiyouzhang@163.com
 */
public class RecommendFragment extends BaseFragment implements OnClickListener{

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;
	private ImageView titleSearch;
	private RefreshLoadmoreLayout layoutRefresh;
	private XtomListView listview;//推荐商品
	private LinearLayout ll_return_top;
	
	private ArrayList<YijiaRecommend> recommends = new ArrayList<YijiaRecommend>();//推荐商品
	private RecommendAdapter adapter;
	private int page = 0;
    private int ten;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_cmn_refresh_list_title);
		super.onCreate(savedInstanceState);
		ten = BaseUtil.dip2px(getActivity(), 10);
		getRecommodGoods();
	}
	
	/**获取推荐商品列表
	 * 
	 */
	private void getRecommodGoods() {
		getNetWorker().yijiaRecommendList("", page+"");
	}
	
	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case YIJIA_RECOMMEND:
			showProgressDialog("加载中");
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case YIJIA_RECOMMEND:
			cancelProgressDialog();
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case YIJIA_RECOMMEND:
			HemaPageArrayResult<YijiaRecommend> bResult = (HemaPageArrayResult<YijiaRecommend>) baseResult;
			if (bResult == null || bResult.getObjects() == null)
				return;
//			showProgress = true;
			if (page == 0) {// 刷新
				layoutRefresh.refreshSuccess();
				recommends.clear();
				this.recommends.addAll(bResult.getObjects());
			}else {
				layoutRefresh.loadmoreSuccess();
				if (bResult.getObjects().size()>0) {
					this.recommends.addAll(bResult.getObjects());
				}else {
					layoutRefresh.setLoadmoreable(false);
					XtomToastUtil.showShortToast(getActivity(), "已经到最后啦");
				}
			}
			refreshData(-1);
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case YIJIA_RECOMMEND:
//			showProgress = true;
			if (page == 0) {// 刷新
				layoutRefresh.refreshFailed();
			}else {
				layoutRefresh.loadmoreFailed();
			}
            XtomToastUtil.showShortToast(getActivity(), "操作失败,"+baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case YIJIA_RECOMMEND:
//			showProgress = true;
			if (page == 0) {// 刷新
				layoutRefresh.refreshFailed();
				refreshData(failedType);
			}else {
				layoutRefresh.loadmoreFailed();
				refreshData(failedType);
			}
            XtomToastUtil.showShortToast(getActivity(), "操作失败，请稍后再试");
			break;
		default:
			break;
		}
	}
	
	private void refreshData(int failedType) {
		if (adapter == null) {
			adapter = new RecommendAdapter(getActivity(), recommends);
			adapter.setEmptyString("暂时没有推荐");
			listview.setAdapter(adapter);
		}else {
			adapter.setEmptyString("暂时没有推荐");
			adapter.notifyDataSetChanged();
		}
		adapter.setFailtype(failedType);
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);
		titleSearch = (ImageView) findViewById(R.id.title_right_img);

		layoutRefresh = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		listview = (XtomListView) findViewById(R.id.listview);
		ll_return_top = (LinearLayout) findViewById(R.id.ll_return_top);//回到顶部
	}

	@Override
	protected void setListener() {
		titleText.setText("易家推荐");
		titleLeft.setVisibility(View.GONE);
		titleRight.setVisibility(View.GONE);
		titleSearch.setVisibility(View.GONE);
		titleSearch.setOnClickListener(this);

		layoutRefresh.setOnStartListener(new StartListener());
		ll_return_top.setOnClickListener(this);
	}

	private class StartListener implements OnStartListener {

		@Override
		public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
//			showProgress = false;
			page = 0;
			getRecommodGoods();
		}

		@Override
		public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
//			showProgress = false;
			page++;
			getRecommodGoods();
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_right_img:
			Intent itSearch = new Intent(getActivity(), SearchActivity.class);
			itSearch.putExtra("type", 8);
			itSearch.putExtra("assort", "粮库");
			startActivity(itSearch);
			break;
		case R.id.ll_return_top:
			listview.setSelection(0);
			break;
		default:
			break;
		}
	}

}
